/*
 * @Author: chunlai
 * @Date: 2024-11-18 16:16:50
 * @LastEditTime: 2025-11-27 15:51:20
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\mixins\rrweb.js
 * @Description:录屏
 */
import { saveUUID } from "@/utils/functions.js";
export const rrwebMixin = {
  data() {
    return {
      rrwebFn: "",
      events: [],
      emitTime: 0,
    };
  },
  methods: {
    openRrweb() {
      if (this.gysEngName == "pingan") {
        //开始录屏
        try {
          var Api_rrweb = "";
          if (process.env.VUE_APP_PRODFLAG === "wxou") {
            // 生产域名
            Api_rrweb =
              "https://bd-record.jkb.pingan.com/recorder-server/recorder/insertRecorder";
          } else {
            Api_rrweb =
              "https://bd-record-stg1.jkb.pingan.com/recorder-server/recorder/insertRecorder";
          }
          PAHealthRRWeb.startRecord(Api_rrweb, {
            interval: 3000,
          });
          const uuid2 = this.$getMessage("session_record_uuid_key");
          if (uuid2) {
            // this.saveRrwebUuid(uuid2);
          }
        } catch (e) {}
      }
      if (process.env.NODE_ENV === "development") return;
      if (process.env.VUE_APP_PRODFLAG === "wxsc") return;
      let that = this;
      this.rrwebFn = rrweb.record({
        emit(event) {
          that.events.push(event);
          if (that.events.length > 10) {
            that.sendDatas(that.events);
          }
          const now = new Date().getTime();
          if (that.emitTime && now - that.emitTime > 1000) {
            that.sendDatas(that.events);
          }
          if (!that.rrwebFn) {
            that.sendDatas(that.events);
          }
        },
      });
    },
    sendDatas(datas = []) {
      if (datas.length === 0) return;
      const dtime = datas[0] && datas[0].timestamp;
      let params = {
        workNum: this.workNum,
        orderNum: this.orderNum,
        timeOrder: dtime,
        kind: this.rrwebType || "20", // 回溯视屏类型
        unionId: "", // 微信端
        info: JSON.stringify(datas),
      };
      // console.log(params);
      if (!params.workNum || !params.orderNum) return;
      this.events = [];
      this.emitTime = new Date().getTime();
      this.$http
        .post(`https://wxou.mschn.cn/msmicro/backtrack/v1/save`, params, {
          showDialog: false,
        })
        .then((res) => {
          // console.log(res);
        })
        .catch((error) => {
          // console.log(error);
        });
    },
    saveRrwebUuid(uuid) {
      let orderNum = this.$getDataFromUrl("orderNum");
      // console.log("saveRrwebUuid", orderNum);
      if (!orderNum) return; // 继续投保
      let params = {
        workNum: this.workNum,
        orderNum: orderNum,
        orderUUID: uuid,
      };
      saveUUID(params)
        .then(({ data }) => {})
        .catch((err) => {
          console.log(err);
        });
    },
    scrollIntoView() {
      this.defaultPhoneHeight = window.innerHeight;
      window.addEventListener("resize", () => {
        window.setTimeout(function () {
          let ua = navigator.userAgent;
          let iOS = /iPad|iPhone|iPod/.test(ua);
          this.nowPhoneHeight = window.innerHeight;
          if (this.nowPhoneHeight < this.defaultPhoneHeight - 50) {
            if (iOS) {
              document.body.scrollTop = document.body.scrollHeight + 50;
            } else {
              input.scrollIntoView(true);
            }
          }
        }, 50);
      });
    },
  },
};
